#!/usr/bin/env ruby

require File.expand_path('../../config/boot',  __FILE__)
require 'yaml'

class_name = $*[1]

attrs = []
args_attrs = $*[2..-1]

args_attrs.each do |att|
  att_split = att.split(':')
  attrs << att_split[0]
end

conn_conf = YAML.load_file("#{Rails.root}/vendor/plugins/active_unreal/config/conn.conf")

host = conn_conf['host']
port = conn_conf['port']


arq = File.open("#{Rails.root}/app/models/#{class_name.downcase}.rb", 'w')

str = []
str << "class #{class_name} < ActiveUnreal::Base\n"

str << (port == nil ? "  connection UnrealDB::Connection.new('#{host}')\n" : "  connection UnrealDB::Connection.new('#{host}', #{port})\n")

attrs.each do |att|
str << "  has '#{att}'\n"
end
str << "  attr_document_id '#{attrs.first}'\n"
str << 'end'

str.join

arq.write(str)

arq.close

require 'commands/generate'

